import maya.cmds as cmds
import mount_2826_6 as mount;
cmds.SelectAll();
cmds.Delete();
length = 50;
height = 10.0;
taperAmount = 1.6619/4.3146;

baseHoleRadius = (3.2/2.0); #hole width is 3 mm using 4.5 for fudge factor
baseHoleWidth = baseHoleRadius*2.0;
closerHoleDistance = 46.23 + baseHoleWidth;
fartherHoleDistance = 68.74+ baseHoleWidth;
frontToBackDistance = 32.84 + baseHoleWidth;
firstHoleYoffset = 5.6; #distance from hole closest to back of mount to back of mount


wallThickness = 1.1;
#--motor mount specifics
motorMountZ = 26.0;
motorBoxWidth = 27.0;
motorBoxHeight = motorBoxWidth / 2.0 + motorMountZ;
frontMotorMountHeight = 2.857;
frontMotorMountWidth = 33.238;

def createStrykerCurve():
    curveFace = cmds.polyCreateFacet( p=[
    (-4.3146,0,0),
    (-3.6198,0.0339,0),
    (-2.6207,0.4996,0),
    (-1.6215,0.8975,0),
    (-0.6224,1.0669,0),
    (0.6224,1.0669,0),
    (1.6215,0.8975,0),
    (2.6207,0.4996,0),
    (3.6198,0.0339,0),
    (4.3146,0,0)]);
    
    cmds.polyExtrudeFacet((curveFace[0] + '.f[0]'), thickness= length/10,d=4);#divide by 10 for conversion
    
    latticeDx = 10;
    latticeDy = 2;
    latticeDz = 5;  
    cmds.select(curveFace[0]);
    cmds.lattice( dv=(latticeDx,latticeDy, latticeDz), oc=True );
    handle = cmds.ls(sl=True)#work around for the fact twhat non polys don't have curve3[0] format
    handleObject = handle[0].replace("Lattice","")
    cmds.setAttr(handleObject + '.local',1);#necessary for 1 to 1 movement
     
    curveModifier = 9.682;# how far to come down
    startHeight = 10.669;
    middleHeight = 8.107;
    endHeight = 7.519;
    
    controlPointY = (middleHeight - (0.25*startHeight + 0.25*endHeight))*2.0;
    
    curveAmount = 0;#in percent of difference
    
    for i in range(0,latticeDz):
        cmds.select(handle[0] + '.pt[0:' + str(latticeDx-1) + '][1:' + str(latticeDy-1) + '][' + str(i) + ':' + str(i) + ']');
        scaledInput = i/float(latticeDz-1);#scales to -1 to 1
        position = (1-scaledInput)*(1-scaledInput)*startHeight + 2*(1-scaledInput)*scaledInput*controlPointY + scaledInput*scaledInput*endHeight;
        print(i,scaledInput,position,middleHeight);
        cmds.move(position,y=True);
        
    cmds.select(curveFace[0]);
    cmds.lattice( dv=(2,2,2), oc=True );
    handle = cmds.ls(sl=True)#work around for the fact twhat non polys don't have curve3[0] format
    handleObject = handle[0].replace("Lattice","")
    cmds.setAttr(handleObject + '.local',1);#necessary for 1 to 1 movement
    cmds.select(handle[0] + '.pt[0:1][1][1]');
    cmds.move(2.857,y=True);#move to proper height   
    cmds.select(handle[0] + '.pt[0][0:1][1]');
    cmds.move(-16.619,x=True);#move front in on one side
    cmds.select(handle[0] + '.pt[1][0:1][1]');
    cmds.move(16.619,x=True);#move front in on other side
         
    cmds.delete(curveFace[0], constructionHistory=True);#need to delete history to make things permanent
    cmds.select(curveFace[0]);
    return curveFace;




# cmds.setAttr(handle[0] + '.rotateX',90);
# cmds.setAttr(handle[0] + '.translateY',0);
# cmds.setAttr(handle[0] + '.scaleY',5.0);
# cmds.setAttr(handle[0] + '.translateZ',0);

#cmds.select(curveFace[0]);
#cmds.delete(curveFace[0], constructionHistory=True);#need to delete history to make things permanent
  
#second curve for linear squishing
# cmds.nonLinear(type='flare',curve=-0.0,ds=1);
# handle = cmds.ls(sl=True)#work around for the fact that non polys don't have curve3[0] format
# handleObject = handle[0].replace("Handle","")
# cmds.setAttr(handle[0] + '.rotateX',90);
# cmds.setAttr(handle[0] + '.translateY',0);
# cmds.setAttr(handle[0] + '.scaleY',5.0);
# cmds.setAttr(handle[0] + '.translateZ',0);

    
    # Lock an attribute to prevent further modification
    #cmds.setAttr( handle[0] + '.endFlareZ', lock=False )#need to unlock first
    
    #cmds.polyExtrudeFacet( (curveFace[0] + '.f[0]'),  kft=False, ltz=2, inputCurve=curve3[0],divisions=10,taper=taperAmount,ch=True);
#    return curveFace; 


vertCone = cmds.polySphere(r=motorBoxHeight);
vertCone2 = cmds.polyCylinder(r=motorBoxHeight,h=motorBoxHeight*2,sx=40);#will be trimmed later
cmds.rotate('90deg',0,0);
cmds.move(0,0,motorBoxHeight);
cmds.select(vertCone2[0],vertCone[0]);#order matters, try it the other way and it won't work.
vertCone = cmds.polyBoolOp(op=1,ch=1);
cmds.scale(0.5,1,1);
 
boundingBox = cmds.polyCube(w=motorBoxHeight, h=motorBoxHeight,sy=1,d=length);
cmds.move(0,motorBoxHeight/2,-length/2);
cmds.select(vertCone[0],boundingBox[0]);
cmds.align(z='min',alignToLead=True);
cmds.select(boundingBox[0],vertCone[0]);
vertCone = cmds.polyBoolOp(op=3,ch=1);

aBoxLength = 50
aBoxWidth = 85
aBoxThickness = 0.5
A = createStrykerCurve();
#cmds.rotate(0,'90deg',0);

def createPlate(thickness): 
    basePlate = cmds.polyCreateFacet( p=[
    (-3.1875,0,2.5),
    (-3.7655,0,1.5),
    (-4.1395,0,0.5),
    (-4.3095,0,-0.5),
    (-4.2755,0,-1.5),
    (-4.0375,0,-2.5),
    (4.0375,0,-2.5),
    (4.2755,0,-1.5),
    (4.3095,0,-0.5),
    (4.1395,0,0.5),
    (3.7655,0,1.5),
    (3.1875,0,2.5),
    ]);


  
  
    cmds.polyExtrudeFacet((basePlate[0] + '.f[0]'), thickness= thickness);#divide by 10 for conversion
    basePlate = cmds.ls(sl=True)#work around for the fact that non polys don't have curve3[0] format
    cmds.select(basePlate[0],A[0]);
    cmds.align(z='max',alignToLead=True);
    cmds.align(y='min',alignToLead=True);
    return basePlate;

basePlate = createPlate(aBoxThickness/10.0);
cmds.select(basePlate[0],A[0]);   
A = cmds.polyBoolOp(op=1,ch=1,useThresholds=1);


baseSub = createPlate(motorBoxHeight/10.0);
cmds.select(baseSub[0],A[0]);   
A = cmds.polyBoolOp(op=3,ch=1,useThresholds=1);

# cmds.select(vertCone[0]);

# 

B = createStrykerCurve();
# 
cmds.move(0,-aBoxThickness,0,relative=True);
# 
cmds.select(A[0],B[0]);
A = cmds.polyBoolOp(op=2,ch=0,useThresholds=1);

C = createStrykerCurve();
cmds.select(vertCone[0],C[0])
vertCone = cmds.polyBoolOp(op=2,ch=0,useThresholds=1);
cmds.move(0,-0.01,0,relative=True);

cmds.select(vertCone[0],A[0]);
A = cmds.polyBoolOp(op=1);

diff = cmds.polyCube(w=motorBoxWidth, h=motorBoxWidth,d=length);
cmds.select(diff[0],A[0]);
cmds.align(z='max',alignToLead=True);
cmds.align(y='max',alignToLead=True);
cmds.select(diff[0]);
cmds.move(0,0,-wallThickness,relative=True);


# A_temp = cmds.duplicate(A[0]);
# diff_temp = cmds.duplicate(diff[0]);
# cmds.select(A_temp[0],diff_temp[0]);
# plug = cmds.polyBoolOp(op=3);

cmds.select(A[0],diff[0]);
A = cmds.polyBoolOp(op=2);

# oldbase was at 25, cylinder is at 16.2, diff = 8.8
hole1 = cmds.polyCylinder(r=baseHoleRadius,h=(wallThickness*3),sx=40);
cmds.move((-0.5*closerHoleDistance),0,( - baseHoleWidth - firstHoleYoffset));

hole2 = cmds.polyCylinder(r=baseHoleRadius,h=(wallThickness*3),sx=40);
cmds.move((0.5*closerHoleDistance),0,( - baseHoleWidth - firstHoleYoffset));

hole3 = cmds.polyCylinder(r=baseHoleRadius,h=(wallThickness*3),sx=40);
cmds.move((0.5*fartherHoleDistance),0,( - baseHoleWidth - firstHoleYoffset - frontToBackDistance));

hole4 = cmds.polyCylinder(r=baseHoleRadius,h=(wallThickness*3),sx=40);
cmds.move((-0.5*fartherHoleDistance),0,( - baseHoleWidth - firstHoleYoffset - frontToBackDistance));

for h in [hole1[0],hole2[0],hole3[0],hole4[0]]:
    cmds.select(A[0],h);
    A = cmds.polyBoolOp(op=2,useThresholds=1);
    
motorMount = mount.createMotorMount();
cmds.select(motorMount[0]);
cmds.move(0,motorMountZ,0);
cmds.rotate(90,0,0);
cmds.move(0,0,-mount.wallthickness_mount,relative=True);

cmds.select(A[0],motorMount[0]);
cmds.polyBoolOp(op=2);
cmds.rotate(0,180,0);







